* This dofile creates the figures and tables shown in the main paper.

***** Figures *****

// Figure 1
foreach level in "indiv" "party" "orient"{

use "$output/database_win", clear

keep if main_sample_`level'==1 //main sample for each level of analysis

rdplot treat_pred_`level' running, p(2) nbins(20 20) ///
graph_options(xtitle(Running variable) ytitle(Predicted treatment status) graphregion(color(white)) legend(off))
graph export "$main/Figure1_`level'.png", replace	
}

// Figure 2
use "$output/database_win", clear

keep if main_sample_indiv==1 

rdplot win_indiv_next running, p(2) nbins(20 20) ///
graph_options(xtitle(Running variable) ytitle("Cand. wins, t+1") graphregion(color(white)) legend(off))
graph export "$main/Figure2_indiv.png", replace	

use "$output/database_win", clear

keep if main_sample_party==1 

rdplot win_party_next running, p(2) nbins(20 20) ///
graph_options(xtitle(Running variable) ytitle("Party wins, t+1") graphregion(color(white)) legend(off))
graph export "$main/Figure2_party.png", replace

use "$output/database_win", clear

keep if main_sample_orient==1 

rdplot win_orient_next running, p(2) nbins(20 20) ///
graph_options(xtitle(Running variable) ytitle("Orient. wins, t+1") graphregion(color(white)) legend(off))
graph export "$main/Figure2_orient.png", replace

// Figure 3
use "$output/database_win", clear

keep if main_sample_indiv==1 

rdplot running_indiv_next running, p(2) nbins(20 20) ///
graph_options(xtitle(Running variable) ytitle("Cand. runs, t+1") graphregion(color(white)) legend(off))
graph export "$main/Figure3_indiv.png", replace	

use "$output/database_win", clear

keep if main_sample_party==1 

rdplot running_party_next running, p(2) nbins(20 20) ///
graph_options(xtitle(Running variable) ytitle("Party runs, t+1") graphregion(color(white)) legend(off))
graph export "$main/Figure3_party.png", replace

use "$output/database_win", clear

keep if main_sample_orient==1

rdplot nb_othercands_orient_R1_next running, p(2) nbins(20 20) ///
graph_options(xtitle(Running variable) ytitle("Orient. nb. other cand., t+1") graphregion(color(white)) legend(off))
graph export "$main/Figure3_orient_1.png", replace

use "$output/database_win", clear

keep if main_sample_orient==1

rdplot nb_cands_orient_R1_next running, p(2) nbins(20 20) ///
graph_options(xtitle(Running variable) ytitle("Orient. nb. all cand., t+1") graphregion(color(white)) legend(off))
graph export "$main/Figure3_orient_2.png", replace

// Figure 4
use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

rdplot win_indiv_next running if running<=0.125, p(2) nbins(12 12) ///
graph_options(ytitle("Cand. wins, t+1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$main/Figure4_win.png", replace	

rdplot running_indiv_next running if running<=0.125, p(2) nbins(12 12) ///
graph_options(ytitle("Cand. runs, t+1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$main/Figure4_run.png", replace	

rdplot qualif_indiv_next running if running<=0.125, p(2) nbins(12 12) ///
graph_options(ytitle("Cand. qualif., t+1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$main/Figure4_qualif.png", replace	

use "$output/database_runoff", clear

keep if main_sample_orient3==1 & assignment_type==3

rdplot nb_othercands_orient_R1_next running if running<=0.125, p(2) nbins(12 12) ///
graph_options(ytitle("Orient. nb. other cand., t+1") xtitle(Running variable) graphregion(color(white)) legend(off))
graph export "$main/Figure4_orient.png", replace		

***** Tables *****

// Table 1
estimates clear

foreach level in "indiv" "party" "orient"{

use "$output/database_win", clear

keep if main_sample_`level'==1

eststo: rdrobust treat_pred_`level' running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum treat_pred_`level' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

esttab using "$main/Table1.tex", b(3) se nonotes booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("Predicted treatment status", pattern(1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) mtitles("Cand." "Party"  "Orient." )	 
// NB: we change the RD_Estimate label and add significance stars based on robust p-value by hand

// Table 2
estimates clear

foreach level in "indiv" "party" "orient"{

use "$output/database_win", clear

keep if main_sample_`level'==1

eststo: rdrobust win_`level'_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum win_`level'_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

esttab using "$main/Table2.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("\shortstack{Cand. wins,\\t+1}" "\shortstack{Party wins,\\t+1}" "\shortstack{Orient. wins,\\t+1}", pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
 
// Table 3
estimates clear

use "$output/database_win", clear		

keep if main_sample_indiv==1
		
eststo: rdrobust running_indiv_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_indiv_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
		
use "$output/database_win", clear
			
keep if main_sample_party==1
		
eststo: rdrobust running_party_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum running_party_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
		
use "$output/database_win", clear
	
keep if main_sample_orient==1
		
eststo: rdrobust nb_othercands_orient_R1_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othercands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

eststo: rdrobust nb_cands_orient_R1_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_cands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)

esttab using "$main/Table3.tex", b(3) se stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) nonotes nomtitles booktabs nocon nostar nonotes replace	 mgroups("\shortstack{Cand. runs,\\t+1}" "\shortstack{Party runs,\\t+1}" "\shortstack{Orient. nb. other\\cand., t+1}" "\shortstack{Orient. nb. all\\cand., t+1}", pattern(1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

// Table 4
estimates clear

use "$output/database_win", clear
		
keep if main_sample_indiv==1

eststo: bootstrap upper_bound=e(upper_bound) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("win_indiv_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.05
sum win_indiv_next if running>=-0.05 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)

use "$output/database_win", clear

keep if main_sample_indiv==1
		
eststo: bootstrap upper_bound=e(upper_bound) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("votesh_indiv_R1_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.05
sum votesh_indiv_R1_next if running>=-0.05 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)

use "$output/database_win", clear

keep if main_sample_orient==1
		
eststo: bootstrap upper_bound=e(upper_bound2) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next_alt, outcome_next("nb_othercands_orient_R1_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.05
sum nb_othercands_orient_R1_next if running>=-0.05 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)
	
esttab using "$main/Table4.tex", replace b(3) se noobs nomtitles nonotes booktabs star(* 0.10 ** 0.05 *** 0.01) ///
coeflabels(upper_bound "Upper bound" lower_bound "Lower bound") mgroups("Cand. wins, t+1" "Cand. vote share,  t+1" "\shortstack{Orient. nb. other cand., t+1}", pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(N_reps mean_control, fmt(0 3) label( "Bootstrap replications" "Mean, left of threshold"))

// Table 5
estimates clear

use "$output/database_win", clear

keep if main_sample_indiv==1

eststo: bootstrap upper_bound=e(upper_bound) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("totexp_indiv_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.05
sum totexp_indiv_next if running>=-0.05 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)

use "$output/database_win", clear

keep if main_sample_indiv==1

eststo: bootstrap upper_bound=e(upper_bound) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("totcontrib_indiv_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.05
sum totcontrib_indiv_next if running>=-0.05 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)		

use "$output/database_win", clear

keep if main_sample_indiv==1

egen temp=sd(originality_indiv_next) 
replace originality_indiv_next=originality_indiv_next/temp
drop temp

eststo: bootstrap upper_bound=e(bound_10pct) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("originality_indiv_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.05
sum originality_indiv_next if running>=-0.05 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)
//NB: for this outcome we use the 10th pctile of the outcome at the left of threshold to compute the lower bound

use "$output/database_win", clear

keep if main_sample_indiv==1

eststo: bootstrap upper_bound=e(bound_10pct) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("sh_perso_indiv_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.05
sum sh_perso_indiv_next if running>=-0.05 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)

use "$output/database_win", clear

keep if main_sample_indiv==1

eststo: bootstrap upper_bound=e(bound_10pct) lower_bound=e(lower_bound), reps($reps) seed(13579) ///
cluster(code_district): leebounds_next, outcome_next("sh_vpp_indiv_next") running_level_next("running_indiv_next") band(0.05)
estadd scalar band=0.05
sum sh_vpp_indiv_next if running>=-0.05 & running<0 & running_indiv_next==1
estadd scalar mean_control=r(mean)


esttab using "$main/Table5.tex", replace b(3) se noobs nomtitles nonotes booktabs star(* 0.10 ** 0.05 *** 0.01) ///
coeflabels(upper_bound "Upper bound" lower_bound "Lower bound") mgroups("\shortstack{Expenditures,\\ t+1}" "\shortstack{Contributions,\\ t+1}" "\shortstack{Manifesto originality,\\ t+1}" "\shortstack{First person,\\ t+1}" "\shortstack{Past tense,\\ t+1}", pattern(1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
stats(N_reps mean_control, fmt(0 3) label( "Bootstrap replications" "Mean, left of threshold"))

// Table 6
estimates clear
	
use "$output/database_win", clear

keep if main_sample_orient == 1

gen running_treat = running*treat
reg nb_othernoparty_orient_next running treat running_treat if running>-0.05 & running<0.05
estimates store noparty
reg nb_otherparty_orient_next running treat running_treat  if running>-0.05 & running<0.05
estimates store party
suest noparty party, cluster(code_district)
test [noparty_mean]treat = [party_mean]treat

estadd local p_value = r(p) //robust p-value shown at bottom of Table 6
estadd local chi2 = r(chi2) //Chi-2 statistic shown at bottom of Table 6

estimates clear 	
	 
use "$output/database_win", clear

keep if main_sample_orient == 1
		
eststo: rdrobust nb_othercands_orient_R1_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othercands_orient_R1_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
estadd local Wald_test=""
estadd local Test_result=""

eststo: rdrobust nb_otherparty_orient_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_otherparty_orient_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
estadd local Wald_test="Chi2 "
estadd local Test_result="`chi2'"

eststo: rdrobust nb_othernoparty_orient_next running, h(0.05) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum nb_othernoparty_orient_next if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
estadd local Wald_test="Robust p-value"
estadd local Test_result="`p_value'"

esttab using "$main/Table6.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control Wald_test Test_result, fmt(3 0 0 3 3 3 ) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold" "Wald test (2)-(3)" " ")) replace ///
mgroups("\shortstack{Orient nb. other\\ cand., t+1}" "\shortstack{Orient nb. other\\party cand., t+1}" "\shortstack{Orient. nb. other\\indep. cand., t+1}" , pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
// NB: we integrate the Chi-2 and p-values and format the table manually

// Table 7
estimates clear

use "$output/database_runoff", clear

keep if main_sample_indiv3==1 & assignment_type==3

foreach y in win_indiv_next running_indiv_next qualif_indiv_next{

eststo: rdrobust `y' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}
	
use "$output/database_runoff", clear

keep if main_sample_orient3==1 & assignment_type==3

foreach y in nb_othercands_orient_R1_next nb_cands_orient_R1_next{

eststo: rdrobust `y' running, h(0.025) vce(nncluster code_district)
estadd scalar rob_p=e(pv_rb)
estadd scalar poly=e(p)
estadd scalar band=e(h_l)
estadd local obs=e(N_h_l)+e(N_h_r)
sum `y' if running>=-e(h_l) & running<0
estadd scalar mean_control=r(mean)
}

esttab using "$main/Table7.tex", b(3) se nonotes nomtitles booktabs nocon nostar nonotes ///
stats(rob_p obs poly band mean_control, fmt(3 0 0 3 3) labels( "Robust p-value" "Observations" "Polynomial order" "Bandwidth" "Mean, left of threshold")) replace ///
 mgroups("\shortstack{Cand. wins,\\t+1}" "\shortstack{Cand. runs,\\t+1}" "\shortstack{Cand. qualif.,\\t+1}" "\shortstack{Orient nb. other\\cand., t+1}" "\shortstack{Orient nb. all\\cand., t+1}", pattern(1 1 1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))
 
***** Other numbers cited in the text *****

// Section 3.1
use "$output/database_win", clear

byso id_unique: gen temp=_n
count if temp==1
local r=r(N)
drop temp

preserve 
keep if main_sample_indiv==1
byso id_unique: gen temp=_n

count if temp==1 //number of races in candidate-level sample

count if temp==1 & election_type==1 //number of parliamentary races in candidate-level sample

count if temp==1 & election_type==2 //number of local races in candidate-level sample

count if temp==1 & abs(running)<0.05 //number of races in which the vote share margin is lower than 5pp

restore

preserve
keep if main_sample_party==1
byso id_unique: gen temp=_n
count if temp==1 //number of races in party-level sample
restore

preserve
keep if main_sample_orient==1
byso id_unique: gen temp=_n
count if temp==1 //number of races in party-level sample
restore

keep if party!=""
byso id_unique: gen temp=_n
count if temp>1 & flag_same_party==1
di r(N)/`r' //fraction of top two contenders of the same party

count if nb_cands_party_R1_next>1 & nb_cands_party_R1_next!=.
di r(N)/`r' //fraction of top two contenders linked to several candidates of the same party at t+1 (footnote 10)

// Section 4.2
use "$output/database_all", clear

byso id_unique: gen temp=_n
count if temp==1
local r=r(N)

drop temp

keep if party!="" & nb_cands_party_R1>1
byso id_unique: gen temp=_n
count if temp==1
di r(N)/`r' //fraction of races with more than one candidate per party (footnote 26)

// Section 4.3
use "$output/database_win", clear
		
keep if main_sample_indiv==1

rdrobust win_indiv_next running if running_indiv_next==1, h(0.05) vce(nncluster code_district)
di e(tau_cl_r) //probability of winning for the close winners who run again 

rdrobust votesh_indiv_R1_next running if running_indiv_next==1, h(0.05) vce(nncluster code_district)
di e(tau_cl_r) //vote share for the close winners who run again 

use "$output/database_win", clear

keep if main_sample_orient==1

rdrobust nb_cands_orient_R1_next running if running_indiv_next==0, h(0.05) vce(nncluster code_district)
di e(tau_cl_l) //number of candidates from the same orientation of close losing candidates who do not run again